var imgNo = "<img width='15px' src='images/n.png'/>";
var imgYes = "<img width='15px' src='images/y.png'>";

function validateTip(element,css,tipString,status){
    element.css(css);
    element.html(tipString);
    element.attr("validateStatus",status);
}

$(function () {
    var userId = $("#id");
    var update = $("#form");
    var loginName = $("#loginName");
    var username = $("#username");
    var identityCode = $("#identity");
    var email = $("#email");
    var mobile = $("#mobile");
    var type = $("#type");

    var t1 = $("#t1");
    var t2 = $("#t2");
    var t3 = $("#t3");
    var t4 = $("#t4");
    //加载用户信息
    $.ajax({
        url : "easybuy/user/getUser",
        type : "post",
        dataType :  "json",
        data : {"id": jq.query.get("id")},
        success : function (data) {
            userId.attr("value",jq.query.get("id"));
            loginName.attr("value",data.loginName);
            username.attr("value",data.userName);
            identityCode.attr("value",data.identityCode);
            email.attr("value",data.email);
            mobile.attr("value",data.mobile);
            if(data.type=="1"){
                type.html("<option value='1' selected>管理员</option><option value='0'>普通用户</option>");
            }else {
                type.html("<option value='0' selected>普通用户</option><option value='1'>管理员</option>");
            }
        },
        error : function () {
            alert("出现错误");
        }
    });

    username.bind("blur",function(){
        var pattern = /^[\u0391-\uFFE5A-Za-z]+$/;
        if(username.val()==null || username.val()== "" ){
            validateTip(t1,{"color": "red"},imgNo+" *姓名不能为空",false);
        }else if(!pattern.test(username.val())){
            validateTip(t1,{"color": "red"}, imgNo +" *输入字符非法",false);
        }else if(username.val().length<2 || username.val().length>20){
            validateTip(t1,{"color": "red"}, imgNo +" *姓名长度错误",false);
        }else {
            validateTip(t1,{"color": "red"}, imgYes,true);
        }
    });

    identityCode.bind("blur", function () {
        var pattern = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;

        if (identityCode.val() == "" || identityCode.val()==null) {
            validateTip(t2, {"color": "red"}, imgNo+" *身份证不能为空", false);
        } else if (!pattern.test(identityCode.val())) {
            validateTip(t2, {"color": "red"}, imgNo+" *身份证格式错误", false);
        } else {
            validateTip(t2, {"color": "red"}, imgYes, true);

        }

    });


    email.bind("blur", function () {
        var re = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
        if (email.val()==null || email.val() == "") {
            validateTip(t3, {"color": "red"}, imgNo+" *邮箱不能为空", false);
        }else if (!re.test(email.val())) {
            validateTip(t3, {"color": "red"}, imgNo+"*邮箱地址格式错误", false);
        } else {
            validateTip(t3, {"color": "red"}, imgYes, true);
        }
    });

    mobile.bind("blur", function () {
        var pattern = /^1[34578]\d{9}$/;
        if (mobile.val() == "" || mobile.val()==null) {
            validateTip(t4, {"color": "red"}, imgNo+" *手机号码不能为空", false);
        } else if (!pattern.test(mobile.val())) {
            validateTip(t4, {"color": "red"}, imgNo+" *手机号码格式错误", false);
        } else {
            validateTip(t4, {"color": "red"}, imgYes, true);
        }
    });

    update.bind("click",function () {
        if(t1.attr("validateStatus") != "true"){
            username.blur();
        }else if(t2.attr("validateStatus") != "true"){
            identityCode.blur();
        }else if(t3.attr("validateStatus") != "true"){
            email.blur();
        }else if(t4.attr("validateStatus") != "true"){
            mobile.blur();
        }else{
            if(confirm("确认提交？")){
                $.ajax({
                    type : "POST",
                    url  : "easybuy/user/update",
                    data : $('#userForm').serialize(),
                    success: function(data){
                        if(data>0){
                            alert("修改成功");
                            window.location.href = "Member_admin.html";
                        }
                    },
                    error: function(){
                        alert("修改失败");
                    }
                });
            }
        }

    });

});